package com.fairytask.ui.effect
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	import com.fairytask.engine.resource.QueueLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;

	/**
	 * 命格动画效果，从一个位置到另一个位置然后消失 
	 * @author xujh
	 * 
	 */
	public class ChartEffect extends Sprite
	{
		private var mc			:Bitmap;	//图片
		private var pointArr	:Array;		//坐标数组
		private var func		:Function;
		/**
		 *  
		 * @param res 图片路径
		 * @param arr 坐标[开始X,开始Y,结束X，结束Y]
		 * @param onComplete 完成时
		 * 
		 */
		public function ChartEffect(res:String,arr:Array,onComplete:Function=null)
		{
			pointArr = arr;
			//加载对象
			QueueLoader.StartLoad(res,onLoad);
			func = onComplete;
		}
		
		/**
		 * 
		 * 
		 */
		private function onLoad(val:Bitmap):void
		{
			mc = val;
			mc.smoothing=true;
			mc.x = pointArr.shift();
			mc.y = pointArr.shift();
			mc.width = 50;
			mc.height = 50;
			addChild(mc);
			playAnime();
		}
		
		/**
		 * 开始播放动画 
		 * 
		 */
		private function playAnime():void
		{
			TweenLite.to(mc, 2, {x:pointArr.shift(), y:pointArr.shift(), ease:Cubic.easeOut,onComplete:onEndPoint});
		}
		
		/**
		 * 到达位置后消失 
		 * 
		 */
		private function onEndPoint():void
		{
			TweenLite.to(mc, 0.2, {onComplete:onClear});
		}
		
		/**
		 * 消失后清除 
		 * 
		 */
		private function onClear():void
		{
			if(null != func)
			{
				func();
			}
			while(this.numChildren>0)
			{
				this.removeChildAt(0);
			}
			if(null != this.parent)
			{
				this.parent.removeChild(this);
			}
		}
	}
}